﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Token.Piker.Core.Photo
{
    public class UploadMap
    {
        private static Hashtable photo_map;
        private static object mutex = new object();
        private UploadMap() { }

        public static Hashtable getInstance()
        {
            if (photo_map == null)
            {
                lock (mutex)
                {
                    if (photo_map == null)
                    {
                        photo_map = new Hashtable();
                        return photo_map;
                    }
                }
            }
            return photo_map;
        }
        /// <summary>
        /// 返回IEnumerable类型
        /// </summary>
        /// <param name="p"></param>
        /// <returns></returns>
        public static IEnumerable<DictionaryEntry> Find(Predicate<DictionaryEntry> p)
        {
            foreach (DictionaryEntry el in UploadMap.getInstance())
            {
                if(p(el))
                    yield return el;
            }
        }
        /// <summary>
        /// 弹出DictionaryEntry元素
        /// </summary>
        /// <param name="p"></param>
        /// <returns></returns>
        public static IEnumerable<DictionaryEntry> Pop(Predicate<DictionaryEntry> p)
        {
            foreach (DictionaryEntry el in UploadMap.getInstance())
            {
                if (p(el))
                {
                    UploadMap.getInstance().Remove(el.Key);
                    yield return el;
                }
                
            }
        }

        public static int Remove(Predicate<DictionaryEntry> p)
        {
            try
            {
                int i = 0;
                foreach (DictionaryEntry el in UploadMap.getInstance())
                {
                    if (p(el))
                    {
                        UploadMap.getInstance().Remove(el.Key);
                        i++;
                    }
                }
                return i;
            }
            catch
            {
                return -1;
            }
        }


        public static bool Add(DictionaryEntry p)
        {
            try
            {
                UploadMap.getInstance().Add(p.Key, p.Value);
                return true;
            }
            catch
            {
                return false;
            }
            
        }

    }
}
